package com.lqd.mall.basis.response;

import com.lqd.mall.basis.code.CommonCode;
import com.lqd.mall.basis.code.CustomCode;
import com.lqd.mall.basis.code.ResultCode;
import com.lqd.mall.basis.constant.ResultMessageConstant;
import lombok.Getter;
import lombok.Setter;

/**
 * @author 零起点
 * @create 2022-01-11 17:15
 */
@Setter
@Getter
public class CommonResult {

    private boolean success;

    private int code;

    private String msg;


    public CommonResult(){}

    public CommonResult(ResultCode resultCode) {
        this.success = resultCode.success();
        this.code = resultCode.code();
        this.msg = resultCode.message();
    }

    /**
     * 默认响应成功数据
     *
     * @return 响应数据
     */
    public static CommonResult SUCCESS() {
        return new CommonResult(CommonCode.SUCCESS);
    }

    /**
     * 自定义返回code
     *
     * @param code 响应code
     * @return 响应数据
     */
    public static CommonResult SUCCESS(int code) {
        return new CommonResult(new CustomCode(true, code, ResultMessageConstant.SUCCESSFUL_MESSAGE));
    }




    public static CommonResult FAIL() {
        return new CommonResult(CommonCode.FAIL);
    }

    public static CommonResult FAIL(int code) {
        return new CommonResult(new CustomCode(false,code,ResultMessageConstant.FAILURE_MESSAGE));
    }
    public static CommonResult FAIL(String message) {
        return new CommonResult(new CustomCode(false,ResultMessageConstant.FAILURE_CODE,message));
    }
}
